﻿using ApiManage.Svc.Filter;
using Autofac.Extensions.DependencyInjection;
using log4net;
using log4net.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Rhonin.Common.Cache;
using Rhonin.Common.Components;
using Rhonin.Common.Configurations;
using Rhonin.Common.Logging;
using Rhonin.Common.ThirdParty.Autofac;
using System;
using System.Reflection;

namespace ApiManage.Svc
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 
        /// </summary>
        public static ILoggerRepository Repository { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            Repository = LogManager.CreateRepository("NETCoreRepository");
        }

        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 
        /// </summary>
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            InitializeQCommon(services);

            return new AutofacServiceProvider(((AutofacObjectContainer)ObjectContainer.Current).Container);
        }

        /// <summary>
        /// 
        /// </summary>
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();

            app.UseStaticFiles(); //使用静态文件wwwroot

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "DemoAPI V1");
                c.RoutePrefix = "swagger";
            });

            app.UseCors(builder =>
            {
                builder.AllowAnyHeader();
                builder.AllowAnyMethod();
                builder.AllowAnyOrigin();
            });
        }

        private void InitializeQCommon(IServiceCollection services)
        {
            var assemblies = new[]
            {
                Assembly.Load("Rhonin.Common"),
                Assembly.Load("ApiManage.Svc"),
                Assembly.Load("ApiManage.Service"),
                Assembly.Load("ApiManage.Models"),
                Assembly.Load("ApiManage.Core"),
                Assembly.Load("ApiManage.Applications")
            };

            Rhonin.Common.Configurations.Configuration
                .Create()
                .UseAutofac()
                .UseJsonNet()
                .UseDefaultCache()
                .UseRedisCache()
                .UseLog4Net()
                .RegisterUnhandledExceptionHandler()
                .RegisterBusinessComponents(services, assemblies)
                .RegisterInterceptors(assemblies)
                .EnableInterceptors(assemblies)
                .RegisterMvcServices(services, Configuration)
                .BuildContainer()
                .InitializeBusinessAssemblies(assemblies);

            var logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);
            logger.Info("Rhonin.Common initialized.");
        }
    }
}
